home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.3 Development Libraries / SGI IRIX 6.3 Development Libraries.iso / dist6.3 / gl_dev.idb / usr / share / src / OpenGL / teach / texture / detail.c.z / detail.c
Encoding:
C/C++ Source or Header  |  1996-12-06  |  3.8 KB  |  155 lines

  1. /*
  2.  * detail - simple program for detail texturing
  3.  *
  4.  * Press n key to move the polygon nearer
  5.  *       f key to move the polygon farther
  6.  */
  7. /* compile: cc -o detail detail.c -lGLU -lGL -lX11 */
  8.  
  9. #include <GL/glx.h>
  10. #include <GL/glu.h>
  11. #include <X11/keysym.h>
  12. #include <stdlib.h>
  13. #include <stdio.h>
  14.  
  15. unsigned int tex[128][128];
  16. unsigned int detailtex[256][256];
  17.  
  18. static void
  19. make_textures(void) {
  20.     int i, j;
  21.     unsigned int *p;
  22.  
  23.     /* base texture is solid gray */
  24.     p = &tex[0][0];
  25.     for (i=0; i<128*128; i++) *p++ = 0x808080ff;
  26.  
  27.     /* detail texture is a yellow grid over a gray background */
  28.     p = &detailtex[0][0];
  29.     for (i=0; i<256; i++) {
  30.     for (j=0; j<256; j++) {
  31.         if (i%8 == 0 || j%8 == 0) {
  32.         *p++ = 0xffff00ff;
  33.         } else {
  34.         *p++ = 0x808080ff;
  35.         }
  36.     }
  37.     }
  38. }
  39.  
  40. static void
  41. init(void) {
  42.     make_textures();
  43.     
  44.     glEnable(GL_TEXTURE_2D);
  45.     glMatrixMode(GL_PROJECTION);
  46.     gluPerspective(90.0, 1.0, 0.3, 10.0 );
  47.     glMatrixMode(GL_MODELVIEW);
  48.     glTranslatef(0.,0.,-1.5);
  49.     
  50.     glClearColor(0.0, 0.0, 0.0, 1.0);
  51.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  52.     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  53.     
  54.     /* note that parameters are applied to the base texture, not the detail */
  55.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  56.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
  57.             GL_LINEAR_DETAIL_SGIS);
  58.     glTexParameteri(GL_TEXTURE_2D, GL_DETAIL_TEXTURE_LEVEL_SGIS, -1);
  59.     glTexImage2D(GL_TEXTURE_2D,
  60.          0, 4, 128, 128, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex);
  61.     glTexImage2D(GL_DETAIL_TEXTURE_2D_SGIS,
  62.          0, 4, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, detailtex);
  63. }
  64.  
  65. static void
  66. draw_scene(void) {
  67.     glClear(GL_COLOR_BUFFER_BIT);
  68.     glBegin(GL_TRIANGLE_STRIP);
  69.         glTexCoord2f( 0, 0); glVertex3f(-1,-0.4, 1); 
  70.         glTexCoord2f( 0, 1); glVertex3f(-1,-0.4,-1); 
  71.         glTexCoord2f( 1, 0); glVertex3f( 1,-0.4, 1); 
  72.         glTexCoord2f( 1, 1); glVertex3f( 1,-0.4,-1); 
  73.     glEnd();
  74.     glFlush();
  75.     
  76. }
  77.  
  78. static void
  79. process_input(Display *dpy) {
  80.     XEvent event;
  81.     Bool redraw = 0;
  82.  
  83.     do {
  84.     char buf[31];
  85.     KeySym keysym;
  86.  
  87.     XNextEvent(dpy, &event);
  88.     switch(event.type) {
  89.       case Expose:
  90.         redraw = 1;
  91.         break;
  92.       case ConfigureNotify:
  93.         glViewport(0, 0, event.xconfigure.width, event.xconfigure.height);
  94.         redraw = 1;
  95.         break;
  96.       case KeyPress:
  97.         (void) XLookupString(&event.xkey, buf, sizeof(buf), &keysym, NULL);
  98.         switch (keysym) {
  99.           case XK_n:
  100.         glTranslatef(0.,0.,.05);
  101.         redraw = 1;
  102.         break;
  103.           case XK_f:
  104.         glTranslatef(0.,0.,-.05);
  105.         redraw = 1;
  106.         break;
  107.           case XK_Escape:
  108.         exit(EXIT_SUCCESS);
  109.           default:
  110.         break;
  111.         }
  112.       default:
  113.         break;
  114.     }
  115.     } while (XPending(dpy));
  116.     if (redraw) draw_scene();
  117. }
  118.  
  119. static void
  120. error(const char *prog, const char *msg) {
  121.     fprintf(stderr, "%s: %s\n", prog, msg);
  122.     exit(EXIT_FAILURE);
  123. }
  124.  
  125. static int attributeList[] = { GLX_RGBA, None };
  126.  
  127. int
  128. main(int argc, char **argv) {
  129.     Display *dpy;
  130.     XVisualInfo *vi;
  131.     XSetWindowAttributes swa;
  132.     Window win;
  133.     GLXContext cx;
  134.  
  135.     dpy = XOpenDisplay(0);
  136.     if (!dpy) error(argv[0], "can't open display");
  137.     vi = glXChooseVisual(dpy, DefaultScreen(dpy), attributeList);
  138.     if (!vi) error(argv[0], "no suitable visual");
  139.     cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
  140.  
  141.     swa.colormap = XCreateColormap(dpy, RootWindow(dpy, vi->screen),
  142.                                    vi->visual, AllocNone);
  143.     swa.border_pixel = 0;
  144.     swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask;
  145.     win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, 800, 800,
  146.             0, vi->depth, InputOutput, vi->visual,
  147.             CWBorderPixel|CWColormap|CWEventMask, &swa);
  148.     XStoreName(dpy, win, "detail");
  149.     XMapWindow(dpy, win);
  150.     glXMakeCurrent(dpy, win, cx);
  151.  
  152.     init();
  153.     while (1) process_input(dpy);
  154. }
  155.